package se.rende.myrss;

import se.rende.myrss.data.MyRSSDataService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class ItemListActivity extends FragmentActivity implements
		ItemListFragment.Callbacks {

	private static final String PREF_SHOW_NEW_ID = "showNew";
	private boolean mTwoPane;
	private boolean showNew;
	private String channelId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		channelId = getIntent().getStringExtra(ItemListFragment.ARG_CHANNEL_ID);
		Log.d("ItemListActivity", "channelId=" + channelId);

		getPrefs();

		setContentView(R.layout.activity_item_list);

		ItemListFragment itemListFragment = (ItemListFragment) getSupportFragmentManager()
				.findFragmentById(R.id.item_list);
		itemListFragment.setShowNew(showNew);
		if (findViewById(R.id.item_detail_container) != null) {
			mTwoPane = true;
			itemListFragment.setActivateOnItemClick(true);
		}

		refreshFragmentList();
	}

	private void getPrefs() {
		SharedPreferences preferences = getPreferences(MODE_PRIVATE);
		this.showNew = preferences.getBoolean(PREF_SHOW_NEW_ID, true);
	}

	private void savePrefs() {
		SharedPreferences preferences = getPreferences(MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean(PREF_SHOW_NEW_ID, showNew);
		editor.commit();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		refreshFragmentList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.channel, menu);
		setShowNewAllMenuState(menu.findItem(R.id.showNewAll));
		return true;
	}

	private void setShowNewAllMenuState(MenuItem item) {
		item.setTitle(showNew ? getString(R.string.show_all)
				: getString(R.string.show_new));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		ItemListFragment itemListFragment = (ItemListFragment) fragmentManager
				.findFragmentById(R.id.item_list);
		switch (item.getItemId()) {
		case R.id.refresh:
			Log.d("ItemListActivity", "channelId=" + channelId);
			Intent intent = new Intent(this, MyRSSDataService.class);
			// Create a new Messenger for the communication back
			Messenger messenger = new Messenger(handler);
			intent.putExtra("MESSENGER", messenger);
			intent.putExtra("channelId", channelId);
			startService(intent);
			return true;
		case R.id.showNewAll:
			showNew = !showNew;
			savePrefs();
			itemListFragment.setShowNew(showNew);
			setShowNewAllMenuState(item);
			refreshFragmentList();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * Used to get results back from service through the messenger
	 */
	private Handler handler = new Handler() {
		public void handleMessage(Message message) {
			Object path = message.obj;
			if (message.arg1 == RESULT_OK && path != null) {
				Toast.makeText(ItemListActivity.this, "Download completed.",
						Toast.LENGTH_LONG).show();
				refreshFragmentList();
			} else {
				Toast.makeText(ItemListActivity.this, "Download failed.",
						Toast.LENGTH_LONG).show();
			}

		}
	};

	private void refreshFragmentList() {
		FragmentManager fragmentManager = getSupportFragmentManager();
		ItemListFragment itemListFragment = (ItemListFragment) fragmentManager
				.findFragmentById(R.id.item_list);
		itemListFragment.refreshList();
	};

	@Override
	public void onItemSelected(String id) {
		if (mTwoPane) {
			Bundle arguments = new Bundle();
			arguments.putBoolean(ItemDetailFragment.ARG_SHOW_NEW_ID, showNew);
			arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
			arguments.putString(ItemDetailFragment.ARG_CHANNEL_ID, channelId);
			ItemDetailFragment fragment = new ItemDetailFragment();
			fragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.item_detail_container, fragment).commit();

		} else {
			Intent detailIntent = new Intent(this, ItemDetailSwipeActivity.class);
			detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
			detailIntent.putExtra(ItemDetailFragment.ARG_CHANNEL_ID, channelId);
			detailIntent.putExtra(ItemDetailFragment.ARG_SHOW_NEW_ID, showNew);
			startActivity(detailIntent);
		}
	}
}
